﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.PageControls.PageCommon;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_TextBoxWithButton:PageForm_SingleInputFieldBase,ICommandElement
    {
        private string _placeholder = "";
        private string _value = "";
        private int _length = -1;
        private ColorPaletteEnum _color = ColorPaletteEnum.info;
        private string _iconCss = "fa-search";
        private bool _isAutoSelect = false;
        private bool _readonly = false;
        private string _hiddenValue = null;
        private string _hiddenName = "";
        private bool _btnDisabled = false;
        private bool _isHideBtnIcon = false;
        private string _btnText = "";
        private InputElementType _inputType= InputElementType.text;

        public PageForm_TextBoxWithButton(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_textboxwithbuttonfield");
        }

        public PageForm_TextBoxWithButton PlaceHolder(string placeholder)
        {
            _placeholder = placeholder;

            return this;
        }

        public PageForm_TextBoxWithButton Value(string value)
        {
            _value = value;

            return this;
        }

        public PageForm_TextBoxWithButton InputType(InputElementType type)
        {
            _inputType = type;

            return this;
        }

        public PageForm_TextBoxWithButton MaxLength(int length)
        {
            _length = length;

            return this;
        }

        public PageForm_TextBoxWithButton ButtonColor(ColorPaletteEnum color)
        {
            _color = color;

            return this;
        }

        public PageForm_TextBoxWithButton ButtonText(string btnText)
        {
            _btnText = btnText;

            return this;
        }

        public PageForm_TextBoxWithButton ButtonIconCss(string iconCss)
        {
            _iconCss = iconCss;

            return this;
        }
        

        /// <summary>
        /// 是否点击文本框后自动选中文本框内的文本
        /// </summary>
        /// <param name="isAutoSelect"></param>
        /// <returns></returns>
        public PageForm_TextBoxWithButton SelectedOnClick(bool isAutoSelect = true)
        {
            _isAutoSelect = isAutoSelect;

            return this;
        }

        /// <summary>
        /// 是否为只读
        /// </summary>
        /// <param name="isReadOnly"></param>
        /// <returns></returns>
        public PageForm_TextBoxWithButton ReadOnly(bool isReadOnly = true)
        {
            _readonly = isReadOnly;

            return this;
        }

        /// <summary>
        /// 按钮是否为禁用
        /// </summary>
        /// <param name="disabled"></param>
        /// <returns></returns>
        public PageForm_TextBoxWithButton ButtonDisabled(bool disabled = true)
        {
            _btnDisabled = disabled;
            return this;
        }

        /// <summary>
        /// 隐藏按钮的前缀图标
        /// </summary>
        /// <param name="isHideIcon"></param>
        /// <returns></returns>
        public PageForm_TextBoxWithButton HideButtonIcon(bool isHideIcon=true)
        {
            _isHideBtnIcon = isHideIcon;
            return this;
        }

        public PageForm_TextBoxWithButton WithHiddenValue(string name,string value)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            _hiddenValue = value;
            _hiddenName = name;

            return this;
        }
        
        protected override void RenderContent(TextWriter writer)
        {

            writer.WriteLine($"<input class=\"form-control field-title\" placeholder=\"{_placeholder}\" {(_isRequired ? "required" : "")} {(_readonly).If("readonly")} data-autoselected=\"{_isAutoSelect}\" {(_length > 0).If($"maxlength=\"{_length}\"")} type=\"{_inputType}\" name=\"{_name}\" value=\"{_value}\" />");
            writer.WriteLine("<span class=\"input-group-btn\">");


            var btnAttr = new TagAttrbute();
            btnAttr.Class("btn btn-" + _color.ToStringEx());

            Command?.RenderToElement(btnAttr);

            if (!string.IsNullOrWhiteSpace(_hiddenName))
            {
                writer.WriteLine($"<input type=\"hidden\" class=\"filed-value\" value=\"{_hiddenValue}\" name=\"{_hiddenName}\" />");
            }

            var iconHtml = "";

            if (_isHideBtnIcon)
            {
                iconHtml = "";
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(_iconCss))
                {
                    iconHtml = $"<i class=\"fa {_iconCss}\"></i>";
                }
            }

            writer.WriteLine($"<{Command?.ReommentTag??"button"} type=\"button\" {_btnDisabled.If("disabled")} {btnAttr}>{iconHtml}{_btnText}</button>");
            writer.WriteLine("</span>");
        }

        protected override string ErrorMessage
        {
            get { return GetErrorMessageByNames(_hiddenName, _name); }
        }

        protected override bool HasError
        {
            get { return IsHasErrorByNames(_hiddenName, _name); }
        }

        public ButtonCmdBase Command { get; set; }
    }
}
